package helper

import (
	"fmt"
	"math/big"
	"strconv"
)

//字节自动转换为B/KB/MB/GB
func ByteToUnitSize(Size interface{}) (float64, string) {
	var unit string
	var iFloat, mb, gb float64 = 0, 1024, 1024 * 1024
	iSize, _ := ToFloat64(Size)
	rSize := iSize / 1024
	if rSize >= 0 && rSize < 1 {
		iFloat, unit = iSize, "B"
	} else if rSize >= 1 && rSize < mb {
		iFloat, unit = rSize, "KB"
	} else if rSize >= mb && rSize < gb {
		iFloat, unit = rSize/1024, "MB"
	} else if rSize >= gb {
		iFloat, unit = rSize/1024/1024, "GB"
	}

	return iFloat, unit
}

/**
 * 数值相加
 * @param str  加数，加数...
 * 2020/05/22
 * gz
 */
func FloatAdd(str ...interface{}) float64 {
	length := len(str)
	if length < 1 {
		return 0
	}
	var i int = 0
	var result, tmp float64 = 0, 0
	var err error
	for i < length {
		tmp, err = ToFloat64(str[i])
		if err == nil {
			result, _ = new(big.Float).Add(new(big.Float).SetFloat64(result), new(big.Float).SetFloat64(tmp)).Float64()
		}
		i++
	}

	result, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", result), 64)
	return result
}

/**
 * float相减
 * @param str  被减数，减数...
 * 2020/05/22
 * gz
 */
func FloatSub(str ...interface{}) float64 {

	length := len(str)
	if length < 1 {
		return 0
	}
	var i int = 1
	result, err := ToFloat64(str[0])
	var tmp float64 = 0

	for i < length {
		tmp, err = ToFloat64(str[i])
		if err == nil {
			result, _ = new(big.Float).Sub(new(big.Float).SetFloat64(result), new(big.Float).SetFloat64(tmp)).Float64()
		}
		i++
	}

	result, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", result), 64)
	return result
}

/**
 * float相乘
 * @param str  被除数，除数，保留小数位数
 * 2020/05/25
 * gz
 */
func FloatMul(str ...interface{}) float64 {

	if len(str) < 2 {
		return 0
	}
	places := "2" //默认两位小数
	if len(str) > 2 {
		places = ToString(str[2])
	}

	float_1, _ := ToFloat64(str[0])
	float_2, _ := ToFloat64(str[1])
	ret, _ := new(big.Float).Mul(new(big.Float).SetFloat64(float_1), new(big.Float).SetFloat64(float_2)).Float64()

	ret, _ = strconv.ParseFloat(fmt.Sprintf("%."+places+"f", ret), 64)
	return ret
}

/**
 * float相除
 * @param str  被除数，除数，保留小数位数
 * 2020/05/25
 * gz
 */
func FloatQuo(str ...interface{}) float64 {
	if len(str) < 2 {
		return 0
	}
	places := "2" //默认两位小数
	if len(str) > 2 {
		places = ToString(str[2])
	}

	float_1, _ := ToFloat64(str[0])
	float_2, _ := ToFloat64(str[1])

	if float_2 <= 0 {
		return 0
	}
	ret, _ := new(big.Float).Quo(new(big.Float).SetFloat64(float_1), new(big.Float).SetFloat64(float_2)).Float64()

	ret, _ = strconv.ParseFloat(fmt.Sprintf("%."+places+"f", ret), 64)
	return ret
}
